<template>
  <!-- sku显示表单 -->
  <el-form>
    <el-form-item label="SKU名称">
      <el-input v-model="skuParams.skuName" />
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input v-model="skuParams.price" />
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input v-model="skuParams.weight" />
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input type="textarea" v-model="skuParams.skuDesc" />
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true">
        <el-form-item
          :label="item.saleAttrName"
          v-for="item in saleArr"
          :key="item.id"
        >
          <el-select v-model="item.saleIdAndValueId">
            <el-option
              :value="`${item.id}:${saleAttrValue.id}`"
              v-for="saleAttrValue in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
              :label="saleAttrValue.saleAttrValueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column
          type="selection"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="图片">
          <template #default="{ row }">
            <img :src="row.imgUrl" alt="" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handler(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <!-- 底部按钮 -->
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button
        type="primary"
        size="default"
        @click="userSpu.toggleTable = '0'"
      >
        取消
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
/* 数据类型约束 */
import type { recordsData } from '@/apis/goods/spu/spuTypes'
import type { skuParam } from '@/apis/goods/spu/spuTypes'
/* 引入请求API */
import { getAllTrait } from '@/apis/goods/trait/index'
import { reqAddSku } from '@/apis/goods/spu/index'
import { getSaleAttrAPI, getSpuImageAPI } from '@/apis/goods/spu/index'
/* 导入仓库 */
import { userSpuState } from '@/store/goods/spu'
/* 获取仓库SPU */
const userSpu = userSpuState()
/* 平台属性 */
const attrArr = ref<any>([])
/* 销售属性 */
const saleArr = ref<any>([])
/* 照片的数据 */
const imgArr = ref<any>([])
/* 获取table组件实例 */
const table = ref<any>()
/* 收集SKU的参数 */
const skuParams = reactive<skuParam>({
  //父组件传递过来的数据
  category3Id: '', //三级分类的ID
  spuId: '', //已有的SPU的ID
  tmId: '', //SPU品牌的ID
  //v-model收集
  skuName: '', //sku名字
  price: '', //sku价格
  weight: '', //sku重量
  skuDesc: '', //sku的描述

  skuAttrValueList: [
    //平台属性的收集
  ],
  skuSaleAttrValueList: [
    //销售属性
  ],
  skuDefaultImg: '', //sku图片地址
})
/* 当前子组件的方法对外暴露 */
const initSkuData = async (
  c1ID: number | string,
  c2Id: number | string,
  spu: recordsData,
) => {
  /* 收集数据 */
  skuParams.category3Id = spu.category3Id
  skuParams.spuId = spu.id
  skuParams.tmId = spu.tmId as string
  /* 获取平台数据 */
  const { data: res }: any = await getAllTrait(c1ID, c2Id, spu.category3Id)
  attrArr.value = res.data
  /* 获取对应的销售属性 */
  const { data: res1 }: any = await getSaleAttrAPI(spu.id as number)
  saleArr.value = res1.data
  /* 获取照片墙的数据 */
  const { data: res2 }: any = await getSpuImageAPI(spu.id as number)
  imgArr.value = res2.data
}
/* 设置默认图片的方法回调 */
const handler = (row: any) => {
  //点击的时候,全部图片的的复选框不勾选
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  })
  //选中的图片才勾选
  table.value.toggleRowSelection(row, true)
  //收集图片地址
  skuParams.skuDefaultImg = row.imgUrl
}
/* 保存按钮的方法 */
const save = async () => {
  //整理参数
  //平台属性
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId,
      })
    }
    return prev
  }, [])
  //销售属性
  skuParams.skuSaleAttrValueList = saleArr.value.reduce(
    (prev: any, next: any) => {
      if (next.saleIdAndValueId) {
        let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
        prev.push({
          saleAttrId,
          saleAttrValueId,
        })
      }
      return prev
    },
    [],
  )
  //添加SKU的请求
  let result: any = await reqAddSku(skuParams)
  console.log(result)
}
defineExpose({ initSkuData })
</script>

<style></style>
